package com.meiji.toutiao.database.dao;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.meiji.toutiao.Constant;
import com.meiji.toutiao.InitApp;
import com.meiji.toutiao.R;
import com.meiji.toutiao.bean.news.NewsChannelBean;
import com.meiji.toutiao.database.DatabaseHelper;
import com.meiji.toutiao.database.table.NewsChannelTable;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Meiji on 2017/3/10.
 * 功能：数据库的操作
 */

public class NewsChannelDao {

	private SQLiteDatabase db;

	public NewsChannelDao() {
		this.db = DatabaseHelper.getDatabase();
	}

	public void addInitData() {
		String categoryId[] = InitApp.AppContext.getResources().getStringArray(R.array.mobile_news_id);  // tablayout中所有的tittle的id
		String categoryName[] = InitApp.AppContext.getResources().getStringArray(R.array.mobile_news_name);  // tablayou中所有的tittle名称
		for (int i = 0; i < 8; i++) {  // 前8个是默认安装的时候，显示的tablayout的个数，先通过add好
			add(categoryId[i], categoryName[i], Constant.NEWS_CHANNEL_ENABLE, i);   //
		}
		for (int i = 8; i < categoryId.length; i++) {  // 后面的tablayout也设置进去
			add(categoryId[i], categoryName[i], Constant.NEWS_CHANNEL_DISABLE, i);
		}
	}

	/**
	 * 功能：忘数据库中插入tablayout中tittle数据
	 *
	 * @param channelId   tablayot 的id 的tittle 的ID
	 * @param channelName tablayout 的tittl的名称
	 * @param isEnable    // 默认显示的是1，不显示的是0
	 * @param position    // tablaout中tittle的位置
	 * @return
	 */
	public boolean add(String channelId, String channelName, int isEnable, int position) {

		ContentValues values = new ContentValues();  // 数据库插入的方法，使用contentValues
		values.put(NewsChannelTable.ID, channelId);
		values.put(NewsChannelTable.NAME, channelName);
		values.put(NewsChannelTable.IS_ENABLE, isEnable);
		values.put(NewsChannelTable.POSITION, position);
		long result = db.insert(NewsChannelTable.TABLENAME, null, values);  //向数据库插入数据，第二个参数，设置null,表明可以vallues的值位空
		return result != -1;   // -1，没有插入
	}

	public List<NewsChannelBean> query(int isEnable) {
		Cursor cursor = db.query(NewsChannelTable.TABLENAME, null, NewsChannelTable.IS_ENABLE + "=?", new String[]{isEnable + ""}, null, null, null);
		List<NewsChannelBean> list = new ArrayList<>();
		while (cursor.moveToNext()) {
			NewsChannelBean bean = new NewsChannelBean();
			bean.setChannelId(cursor.getString(NewsChannelTable.ID_ID));
			bean.setChannelName(cursor.getString(NewsChannelTable.ID_NAME));
			bean.setIsEnable(cursor.getInt(NewsChannelTable.ID_ISENABLE));
			bean.setPosition(cursor.getInt(NewsChannelTable.ID_POSITION));
			list.add(bean);
		}
		cursor.close();
		return list;
	}

	public List<NewsChannelBean> queryAll() {
		Cursor cursor = db.query(NewsChannelTable.TABLENAME, null, null, null, null, null, null);
		List<NewsChannelBean> list = new ArrayList<>();
		while (cursor.moveToNext()) {
			NewsChannelBean bean = new NewsChannelBean();
			bean.setChannelId(cursor.getString(NewsChannelTable.ID_ID));
			bean.setChannelName(cursor.getString(NewsChannelTable.ID_NAME));
			bean.setIsEnable(cursor.getInt(NewsChannelTable.ID_ISENABLE));
			bean.setPosition(cursor.getInt(NewsChannelTable.ID_POSITION));
			list.add(bean);
		}
		cursor.close();
		return list;
	}

	public void updateAll(List<NewsChannelBean> list) {
	}

	public boolean removeAll() {
		int result = db.delete(NewsChannelTable.TABLENAME, null, null);
		return result != -1;
	}
}
